<template>
  <div class="login">
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <h2>天道大品管理台</h2>
      <el-form-item label="用户名" prop="username">
        <el-input
          type="string"
          v-model="ruleForm.username"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          type="password"
          v-model="ruleForm.password"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >提交</el-button
        >
        <el-button @click="resetForm()">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    var passCheck = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("密码不可为空"));
      }
      callback();
    };
    var usernameCheck = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("用户名不可为空"));
      }
      callback();
    };
    return {
      ruleForm: {
        username: "",
        password: "",
      },
      rules: {
        password: [{ validator: passCheck, trigger: "blur" }],
        username: [{ validator: usernameCheck, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.axiosSubmit("/admin/login");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm() {
      this.$refs['ruleForm'].resetFields();
    },
    axiosSubmit(api) {
      this.axios.post(api, this.ruleForm).then((response) => {
        if (response.data === null || response.data === undefined || response.data === "") {
          this.$message({
            showClose: true,
            message: "用户名或密码错误！",
            type: "error",
          });
          // this.$refs['ruleForm'].resetFields();
          return false;
        }
        this.$message({
          showClose: true,
          message: "登录成功，即将跳转页面！",
          type: "success",
        });
        setTimeout(() => {
          this.$router.push("/container/product");
        }, 1000);
      });
    },  // axiosSubmit
  },
};
</script>

<style lang="scss" scoped>
.login {
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  .el-form {
    width: 500px;
    h2 {
      text-align: center;
      height: 50px;
    }
  }
}
</style>